//
//  KTSessionViewController.m
//  KTChat
//
//  Created by Kevin on 2016/11/11.
//  Copyright © 2016年 Kevin. All rights reserved.
//

#import "KTSessionViewController.h"
#import "KTSessionViewModel.h"

@interface KTSessionViewController ()<UITableViewDelegate, UITableViewDataSource, KTMessageTextViewBlankDelegate>

@property (nonatomic, strong) NSLayoutConstraint *inputBarBottomConstraint;
@property (nonatomic, strong) KTSessionViewModel *sessionModel;

@end

@implementation KTSessionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)setupUI
{
    self.view.backgroundColor = [UIColor lightGrayColor];
    // tableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.translatesAutoresizingMaskIntoConstraints = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
    [self.view addSubview:_tableView];
    NSLayoutConstraint *cs = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
    [self.view addConstraint:cs];
    cs = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
    [self.view addConstraint:cs];
    cs = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
    [self.view addConstraint:cs];
    // 输入栏
    _inputBar = [KTMessageInputBar messageInputBar];
    _inputBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_inputBar];
    cs = [NSLayoutConstraint constraintWithItem:_inputBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
    [self.view addConstraint:cs];
    cs = [NSLayoutConstraint constraintWithItem:_inputBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
    [self.view addConstraint:cs];
    self.inputBarBottomConstraint = cs;
    cs = [NSLayoutConstraint constraintWithItem:_inputBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
    [self.view addConstraint:cs];
    cs = [NSLayoutConstraint constraintWithItem:_inputBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_tableView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
    [self.view addConstraint:cs];
    [_inputBar.leftButton addTarget:self action:@selector(tapLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    [_inputBar.rightButton1 addTarget:self action:@selector(tapRightButton1:) forControlEvents:UIControlEventTouchUpInside];
    [_inputBar.rightButton2 addTarget:self action:@selector(tapRightButton2:) forControlEvents:UIControlEventTouchUpInside];
    _inputBar.textView.blankDelegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- actions --

- (void)tapLeftButton:(UIButton *)btn
{
    NSLog(@"点击左边按钮");
}

- (void)tapRightButton1:(UIButton *)btn
{
    NSLog(@"点击右边1按钮");
}

- (void)tapRightButton2:(UIButton *)btn
{
    NSLog(@"点击右边2按钮");
}

#pragma mark -- notifis --

- (void)handleKeyboardWillChange:(NSNotification *)notif
{
    NSDictionary *userInfo = [notif userInfo];
    NSLog(@"%@", userInfo);
    CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    if (CGRectIsNull(keyboardEndFrame)) {
        return;
    }
    UIViewAnimationCurve animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    NSInteger animationCurveOption = (animationCurve << 16);
    double animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    if (fabs(height - keyboardEndFrame.origin.y) < 1.0) {
        self.inputBarBottomConstraint.constant = 0;
    } else {
        self.inputBarBottomConstraint.constant = -keyboardEndFrame.size.height;
    }
    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:animationCurveOption
                     animations:^{
                         [self.view layoutIfNeeded];
                     }
                     completion:nil];
}

#pragma mark -- KTMessageTextViewBlankDelegate --

- (void)messageTextView:(KTMessageTextView *)view didBecomeBlank:(BOOL)blank
{
    if (blank) {
        [self.inputBar.rightButton2 setBackgroundImage:[UIImage imageNamed:@"TypeSelectorBtn_Black"] forState:UIControlStateNormal];
        [self.inputBar.rightButton2 setBackgroundImage:[UIImage imageNamed:@"TypeSelectorBtnHL_Black"] forState:UIControlStateHighlighted];
        [self.inputBar.rightButton2 setTitle:nil forState:UIControlStateNormal];
        [self.inputBar.rightButton2 setTitle:nil forState:UIControlStateNormal];
    } else {
        [self.inputBar.rightButton2 setBackgroundImage:nil forState:UIControlStateNormal];
        [self.inputBar.rightButton2 setBackgroundImage:nil forState:UIControlStateHighlighted];
        [self.inputBar.rightButton2 setTitle:@"发送" forState:UIControlStateNormal];
        [self.inputBar.rightButton2 setTitle:@"发送" forState:UIControlStateNormal];
    }
}

#pragma mark -- table view --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
    cell.textLabel.text = @"ssdsd";
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.inputBar.textView resignFirstResponder];
}

@end
